package com.ruicar.afs.cloud.bizcommon.rules.controller;

import com.ruicar.afs.cloud.bizcommon.dto.rules.atom.AfsRuleAtomDto;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleAtomService;
import com.ruicar.afs.cloud.bizcommon.vo.rules.AfsRuleAtomVo;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Api("规则原子配置接口")
@RestController
@RequestMapping("/ruleAtom")
@AllArgsConstructor
public class AfsRuleAtomController {
    private AfsRuleAtomService afsRuleAtomService;

    @PostMapping("/addAfsRuleAtom")
    @ApiOperation(value = "添加原子")
    @SysLog(value = "添加原子")
    @PreAuthorize("@pms.hasPermission('afs_rule_atom_add')")
    public IResponse<Boolean> addAtom(@RequestBody @Validated AfsRuleAtomVo afsRuleAtomDto) {
        return IResponse.success(afsRuleAtomService.addRuleAtom(afsRuleAtomDto));
    }

    @DeleteMapping("/delAfsRuleAtom/{atomId}")
    @ApiOperation(value = "删除原子")
    @SysLog(value = "删除原子")
    @PreAuthorize("@pms.hasPermission('afs_rule_atom_del')")
    public IResponse<Boolean> delAtom(@PathVariable Long atomId) {
        Assert.isTrue(atomId!=null,"规则原子ID不能为空");
        return IResponse.success(afsRuleAtomService.delRuleAtom(atomId));
    }

    @PostMapping("/modifyAfsRuleAtom")
    @ApiOperation(value = "编辑原子")
    @SysLog(value = "编辑原子")
    @PreAuthorize("@pms.hasPermission('afs_rule_atom_modify')")
    public IResponse<Boolean> modifyAtom(@RequestBody @Validated AfsRuleAtomVo afsRuleAtomDto) {
        return IResponse.success(afsRuleAtomService.modifyRuleAtom(afsRuleAtomDto));
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询原子")
    @SysLog(value = "查询原子")
    public IResponse queryAtomList(@RequestBody QueryCondition<AfsRuleAtomVo> queryCondition) {
        return afsRuleAtomService.query(queryCondition);
    }

    @PostMapping("/queryAtomsByBizType/{bizType}")
    @ApiOperation(value = "按照业务场景获取规则原子（内部调用）")
    public IResponse<List<AfsRuleAtomDto>> queryAtomsByBizType(@PathVariable String bizType) {
        return afsRuleAtomService.queryAtomsByBizType(bizType);
    }
}
